//
//  WBHomeViewModel.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

private let year = 365 * 24 * 60 * 60

class WBHomeViewModel: NSObject {

    var status : WBStatusModel?
    
    
    init(status:WBStatusModel) {
        
        self.status = status
        
        super.init()
        
//        dealSource(source: source)
    }
    
    
    //存储属性
    var source : String?
    
    
    //计算属性
    var source_string : String?{
    
        return dealSource(source: status?.source)
    
    }
    
    
    //来源
    func dealSource(source:String?)->String?{
    
        guard let source = source else {
            return ""
        }
    
        let nsstr = source as NSString
        
        let sourceArray = nsstr.components(separatedBy: ">")
        
        
        if sourceArray.count >= 2 {
            
            let sourceStr = sourceArray[1] as NSString
            
            let lastArray = sourceStr.components(separatedBy: "<")
            
            return lastArray.first
        }
        
        
        return ""
    
    }
    
    
    var time_String: String?{
    
//        return dealTime(created_at: "Sun Dec 04 12:36:44 +0000 2016")
        return dealTime(created_at: status?.created_at)
    }
    
    //时间
    private func dealTime(created_at:String?)->String?{
        
        guard let created_at = created_at else {
            return nil
        }
        
        //创建时间格式化对象
        let formatter = DateFormatter()
        
        //设置时间转换格式
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        //设置时区
        formatter.locale = Locale.init(identifier: "en_US")
        
        //将服务器返回字符串转换成标准时间
        guard  let created_date = formatter.date(from: created_at)else {
            return nil
        }
        
        //获取当前日历
        //        let cal = NSCalendar.current
        
        let cal = Calendar.current
        
        //当前时间和发布时间进行秒数比较
        let seconds = Int(Date().timeIntervalSince(created_date))
        
        
        //判断是否超过一年时间
        if seconds < year {
            
            //判断是否今天
            if cal.isDateInToday(created_date) {
                
                if seconds < 60 {
                    
                    return "刚刚"
                    
                }
                
                if seconds < 60 * 60 {
                    
                    return "\(seconds/60)分钟之前"
                }
                
                return "\(seconds/60/60)小时前"
                
            }else
            
           
            //判断是否是昨天
            if cal.isDateInYesterday(created_date) {
                
                formatter.dateFormat = "HH:mm"
                
                let dateString = formatter.string(from: created_date)
                
                return "昨天\(dateString)"
            }
            
            //其他天数
            formatter.dateFormat = "MM-dd HH:mm"
            
            let dateStr = formatter.string(from: created_date)
            
            return "\(dateStr)"
            
        }
        
        formatter.dateFormat = "yyyy-MM-dd"
    
        return formatter.string(from: created_date)
        
    }
    
    
    
    //转发数
    var reposts_string : String? {
    
        return status?.reposts_count==0 ? "转发" : "\(status!.reposts_count)"
    
    }
    
    //评论数
    var comments_count : String? {
    
        return status?.comments_count==0 ? "评论" : "\(status!.comments_count)"
    }
    
    //点赞数
    var attitudes_count : String? {
        
      return status?.attitudes_count==0 ? "赞" : "\(status!.attitudes_count)"
    
    }
    
    //头像url
    var profile_image_url : URL? {
    
    
        return URL(string: status?.user?.profile_image_url ?? "")
    }
    
    
    //认证图片
    var verified : UIImage? {
    
        if let  verified = status?.user?.verified {
            
            switch verified {
            case -1:
                return UIImage()
            case 1:
                return UIImage(named:"avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return UIImage()
            }
            
        }
    
        return UIImage()
    }
    
    
    //设置等级图片
    var mbrank : UIImage? {
    
        if let mbrank = status?.user?.mbrank {
            
            if mbrank==0 {
                
                return UIImage(named: "common_icon_membership_expired")
            }else if mbrank>0 && mbrank<7 {
            
                return  UIImage(named: "common_icon_membership_level\(mbrank)")
            
            }else{
            
                 return UIImage(named: "common_icon_membership")
            
            }
          
        }
    return UIImage()
    }
    
    
    
}
